/**
 * created by 小卷毛, 2020-05-09 009
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.aifeng.viewmodel

import com.aifeng.model.BaseUrl
import com.aifeng.utils.applyFlowableSchedulers
import com.aifeng.utils.applySchedulers
import com.lzg.okrx.RxSingleHelper
import io.reactivex.Flowable
import io.reactivex.Single
import java.util.concurrent.TimeUnit

/**
 * 项目名称：Hassan_Food
 * 创建人：小卷毛
 * 创建时间：2020-05-09 10:14
 */
class LoginViewModel : BaseViewModel() {

    fun checkCode(code: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.checkInvite,
            params = getParams("inviteCode" to code)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun getCode(phone: String, type: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.getCode,
            params = getParams(
                "phone" to phone,
                "type" to type
            )
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun checkPhone(phone: String, code: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.checkPhone,
            params = getParams(
                "phone" to phone,
                "code" to code
            )
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun getFinish(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = BaseUrl.upgrade,
            params = getParams(*params)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun login(phone: String, pwd: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.login,
            params = getParams(
                "phone" to phone,
                "password" to pwd
            )
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun userInfo(userId: String): Single<String> =
        RxSingleHelper.requestString(
            url = BaseUrl.getUserInfo,
            params = getParams("userid" to userId)
        )
            .compose(applySchedulers())

    fun resetPwd(phone: String, code: String, pwd: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.resetPwd,
            params = getParams(
                "phone" to phone,
                "code" to code,
                "pwd" to pwd
            )
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun countDown(count: Long = 180): Flowable<Long> =
        Flowable.intervalRange(0, count + 1, 0, 1, TimeUnit.SECONDS)
            .compose(applyFlowableSchedulers())

}
